package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.model.dto.SecProblemDto;
import com.mianyang.zhangsan.model.entity.SecAnswer;
import com.mianyang.zhangsan.model.entity.SecProblemInst;
import com.mianyang.zhangsan.service.SecAnswerService;
import com.mianyang.zhangsan.service.SecProAnsService;
import com.mianyang.zhangsan.service.SecProblemInstService;
import com.mianyang.zhangsan.mapper.SecProblemInstMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author jiangjr
*/
@Service
public class SecProblemInstServiceImpl extends ServiceImpl<SecProblemInstMapper, SecProblemInst>
    implements SecProblemInstService{
    @Autowired
    private SecAnswerService answerService;

    @Override
    public List<SecProblemDto> selectProblems(List<Long> problemInsIds) {
        List<SecProblemInst> problemInsts = listByIds(problemInsIds);
        List<Long> problemIds = problemInsts.stream()
                .map(SecProblemInst::getProblemId)
                .collect(Collectors.toList());
        List<SecAnswer> answers = answerService.selectAnsByProblemIds(problemIds);
        List<SecProblemDto> problems = new ArrayList<>(problemInsts.size());
        for (SecProblemInst problemInst : problemInsts) {
            List<SecAnswer> collect = answers.stream()
                    .filter(answer -> problemInst.getProblemId().equals(answer.getProblemId()))
                    .collect(Collectors.toList());
            SecProblemDto problem = SecProblemDto.builder()
                    .problem(problemInst)
                    .answers(collect).build();
            problems.add(problem);
        }
        return problems;
    }
}




